package dao;

import dao.BaseHibernateDAO;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import bean.AtcLog;
import bean.AtcStory;

/**
 * A data access object (DAO) providing persistence and search support for
 * AtcStory entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see bean.AtcStory
 * @author MyEclipse Persistence Tools
 */

public class AtcStoryDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(AtcStoryDAO.class);
	// property constants
	public static final String STORY_DESCRIPTION = "storyDescription";
	public static final String LINK_TO = "linkTo";

	public void save(AtcStory transientInstance) {
		Transaction ts=null;
		log.debug("saving AtcStory instance");
		try {
			ts=getSession().beginTransaction();
			getSession().save(transientInstance);
			log.debug("save successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("save failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}
	public void update(AtcStory transientInstance) {
		Transaction ts=null;
		log.debug("saving User instance");
		try {
			ts=getSession().beginTransaction();
			getSession().update(transientInstance);
			log.debug("save successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null)ts.rollback();
			log.error("save failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}

	public void delete(AtcStory persistentInstance) {
		Transaction ts=null;
		log.debug("deleting AtcStory instance");
		try {
			ts=getSession().beginTransaction();
			getSession().delete(persistentInstance);
			log.debug("delete successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("delete failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}

	public AtcStory findById(java.lang.Integer id) {
		Transaction ts=null;
		AtcStory instance=null;
		log.debug("getting AtcStory instance with id: " + id);
		try {
			ts=getSession().beginTransaction();
			instance = (AtcStory) getSession()
					.get("bean.AtcStory", id);
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("get failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return instance;
	}

	public List findByExample(AtcStory instance) {
		Transaction ts=null;
		List results=null;
		log.debug("finding AtcStory instance by example");
		try {
			ts=getSession().beginTransaction();
			results = getSession().createCriteria("bean.AtcStory").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("find by example failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return results;
	}

	public List findByProperty(String propertyName, Object value) {
		Transaction ts=null;
		List results=null;
		log.debug("finding AtcStory instance with property: " + propertyName
				+ ", value: " + value);
		try {
			ts=getSession().beginTransaction();
			String queryString = "from AtcStory as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			results=queryObject.list();
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("find by property name failed", re);
			throw re;
		}finally{
			closeSession();
		}
		
		return results;
	}

	public List findByStoryDescription(Object storyDescription) {
		return findByProperty(STORY_DESCRIPTION, storyDescription);
	}

	public List findByLinkTo(Object linkTo) {
		return findByProperty(LINK_TO, linkTo);
	}

	public List findAll() {
		Transaction ts=null;
		List results=null;
		log.debug("finding all AtcStory instances");
		try {
			ts=getSession().beginTransaction();
			String queryString = "from AtcStory";
			Query queryObject = getSession().createQuery(queryString);
			results=queryObject.list();
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return results;
	}

	public AtcStory merge(AtcStory detachedInstance) {
		Transaction ts=null;
		AtcStory result =null;
		log.debug("merging AtcStory instance");
		try {
			ts=getSession().beginTransaction();
			result = (AtcStory) getSession().merge(detachedInstance);
			log.debug("merge successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("merge failed", re);
			throw re;
		}finally{
			closeSession();
		}
		return result;
	}

	public void attachDirty(AtcStory instance) {
		Transaction ts=null;
		log.debug("attaching dirty AtcStory instance");
		try {
			ts=getSession().beginTransaction();
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("attach failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}

	public void attachClean(AtcStory instance) {
		Transaction ts=null;
		log.debug("attaching clean AtcStory instance");
		try {
			ts=getSession().beginTransaction();
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
			ts.commit();
		} catch (RuntimeException re) {
			if(ts!=null) ts.rollback();
			log.error("attach failed", re);
			throw re;
		}finally{
			closeSession();
		}
	}
}